<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/9/25 10:41
 * Modified Time: 2019/9/25 10:41
 * Version: 2.0
 */

namespace App\Modules\DataCenter\Services;


use App\Modules\Employee\Repositories\EmployeeRepository;
use App\Modules\Inventory\Repositories\GoodsOrderRepository;
use App\Modules\MemberCard\Repositores\MemberCardFlowRepository;
use App\Modules\Order\Repositories\OrderRepository;
use App\Modules\Order\Repositories\RoyaltyRepository;
use App\Modules\Service\Repositories\ServiceRepository;
use App\Modules\System\Repositories\AmountFlowRepository;
use App\Modules\System\Repositories\DictionaryItemRepository;

class EmployeeAchievementService
{
    protected $employeeRepository;
    protected $orderRepository;
    protected $amountFlowRepository;
    protected $goodsOrderRepository;
    protected $memberCardFlowRepository;
    protected $serviceRepository;
    protected $dictionaryItemRepository;
    protected $royaltyRepository;

    public function __construct(EmployeeRepository $employeeRepository,
                                OrderRepository $orderRepository,
                                AmountFlowRepository $amountFlowRepository,
                                GoodsOrderRepository $goodsOrderRepository,
                                MemberCardFlowRepository $memberCardFlowRepository,
                                ServiceRepository $serviceRepository,
                                DictionaryItemRepository $dictionaryItemRepository,
                                RoyaltyRepository $royaltyRepository)
    {
        $this->employeeRepository = $employeeRepository;
        $this->orderRepository = $orderRepository;
        $this->amountFlowRepository = $amountFlowRepository;
        $this->goodsOrderRepository = $goodsOrderRepository;
        $this->memberCardFlowRepository = $memberCardFlowRepository;
        $this->serviceRepository = $serviceRepository;
        $this->dictionaryItemRepository = $dictionaryItemRepository;
        $this->royaltyRepository = $royaltyRepository;
    }

    public function getEmployeeAchievementHeader($store_id, $user_id, $date_type)
    {
        $end_day = $this->formatDate($date_type);
        $barber_info = $this->employeeRepository->getUserInfoByStoreIdDetails($store_id, $user_id);
        $barber_nick = $barber_info->nick;
        $barber_face = $barber_info->picture?$barber_info->picture->image:null;

        $amount = $this->amountFlowRepository->getBarberAmountFlow($user_id, $store_id, $end_day)->sum('real_price');
        $royalty = $this->amountFlowRepository->getBarberAmountFlow($user_id, $store_id, $end_day)->sum('barber_royalty');
        $ponit_member_rate = $this->orderRepository->getPointMemberRateDate($user_id, $store_id, $end_day);

        $data = [
            'barber_name' => $barber_nick,
            'barber_face' => $barber_face,
            'ponit_member_rate' => $ponit_member_rate,
            'amount' => round($amount,2),
            'royalty' => round($royalty,2)
        ];
        return $data;
    }

    /**
     * 获取服务占比
     * @param $store_id
     * @param $user_id
     * @param $date_type
     * @return array
     */
    public function getEmployeeAchievementServiceRate($store_id, $user_id, $date_type)
    {
        $end_day = $this->formatDate($date_type);
        $service_data = $this->dictionaryItemRepository->getServiceTypeImg()->toArray();
        $data = $this->orderRepository->getServiceRateDate($store_id, $user_id, $end_day)->toArray();
        $all_num = 0;
        $ironing_class_num = 0;
        $cut_class_num = 0;
        $order_class_num = 0;
        foreach ($data as $key => $value) {
            $all_num = $all_num + $value['count'];
            if ($value['type'] == 7 || $value['type'] == 9) {
                $cut_class_num = $cut_class_num + $value['count'];
            }
            if ($value['type'] == 8 || $value['type'] == 10) {
                $ironing_class_num = $ironing_class_num + $value['count'];
            }
            if ($value['type'] == 11) {
                $order_class_num = $order_class_num + $value['count'];
            }
        }
        if ($all_num == 0) {
            return [
                [
                    'name' => '烫染类',
                    'image' => $service_data[2]['image'],
                    'rate' => 0,
                ],
                [
                    'name' => '剪发类',
                    'image' => $service_data[0]['image'],
                    'rate' => 0,
                ],
                [
                    'name' => '其他类',
                    'image' => $service_data[3]['image'],
                    'rate' => 0,
                ],
            ];
        } else {
            $ironing_class_num_rate = round(($ironing_class_num * 100) / $all_num, 2);
            $cut_class_num_rate = round(($cut_class_num * 100) / $all_num, 2);
            $order_class_num_rate = round(($order_class_num * 100) / $all_num, 2);
            $return_data = [
                [
                    'name' => '烫染类',
                    'image' => $service_data[2]['image'],
                    'rate' => $ironing_class_num_rate,
                ],
                [
                    'name' => '剪发类',
                    'image' => $service_data[0]['image'],
                    'rate' => $cut_class_num_rate,
                ],
                [
                    'name' => '其他类',
                    'image' => $service_data[3]['image'],
                    'rate' => $order_class_num_rate,
                ],
            ];
            $rate = array_column($return_data, 'rate');
            array_multisort($rate, SORT_DESC, $return_data);
            return $return_data;
        }


    }


    public function getEmployeeAchievementRoyalty($store_id, $user_id, $date_type)
    {
        $end_day = $this->formatDate($date_type);
        $royalty_statistics = $this->royaltyRepository->getBarberRoyaltyDetailDate($store_id, $user_id, $end_day);

        $service_royalty = $this->royaltyRepository
            ->getBarberRoyaltyServiceDetailDate($store_id, $user_id, $end_day)
            ->get()
            ->toArray();
        $member_card_royalty = $this->royaltyRepository
            ->getBarberRoyaltyMemberCardDetailDate($store_id, $user_id, $end_day)
            ->get()
            ->toArray();
        $goods_order_royalty = $this->royaltyRepository
            ->getBarberRoyaltyGoodsOrderDetailDate($store_id, $user_id, $end_day)
            ->get()
            ->toArray();

        $return_data = [
            [
                'name'=>'服务',
                'amount'=>0,
                'list' => $service_royalty
            ],
            [
                'name'=>'办卡',
                'amount'=>0,
                'list' => $member_card_royalty
            ],
            [
                'name'=>'商品',
                'amount'=>0,
                'list' => $goods_order_royalty
            ],
        ];

        foreach ($royalty_statistics as $key => $value){
            if($value->type==1){
                $return_data[0]['amount'] = $value->amount;
            }elseif ($value->type==2){
                $return_data[1]['amount'] = $value->amount;
            }else{
                $return_data[2]['amount'] = $value->amount;
            }
        }

        return $return_data;
    }

    /**
     * 获取营业额
     * @param $user_id
     * @param $store_id
     * @param $end_day
     * @return mixed
     */
    protected function getAmount($user_id, $store_id, $end_day)
    {
        $orderAmount = $this->orderRepository->getAmountDate($user_id, $store_id, $end_day);
        $saleAmount = $this->goodsOrderRepository->getGoodsSaleAmountDate($user_id, $store_id, $end_day);
        $memberCardAmount = $this->memberCardFlowRepository->getMemberCardSaleAmountDate($user_id, $store_id, $end_day);

        $total = $orderAmount + $memberCardAmount + $saleAmount;
        return $total;
    }

    /**
     * 格式化时间
     * @param $date_type
     * @return false|string
     */
    protected function formatDate($date_type)
    {
        if ($date_type == 0) {
            $end_day = date('Y-m-d', time());
        } else {
            $end_day = date('Y-m', time() - (30 * 86400));
        }
        return $end_day;
    }
}